import rclpy
from rclpy.node import Node
from base_interfaces_demo.msg import SystemStatus
import psutil
import platform
class SysStatusPub(Node):
    def __init__(self,node_name):
        super().__init__(node_name)
        self.status_publisher_=self.create_publisher(
            SystemStatus,"sys_status",10)
        self.timer=self.create_timer(1,self.timer_callback)

    def timer_callback(self):
        cpu_percet=psutil.cpu_percent()
        memory_info=psutil.virtual_memory()
        net_io_couters=psutil.net_io_counters()

        msg=SystemStatus()
        msg.stamp=self.get_clock().now().to_msg()
        msg.host_name=platform.node()
        msg.cpu_percent=cpu_percet
        msg.memory_percent=cpu_percet
        msg.memory_total=memory_info.total/1024/1024
        msg.memory_available=memory_info.available/1024/1024
        msg.net_sent=net_io_couters.bytes_sent/1024/1024
        msg.net_recv=net_io_couters.bytes_recv/1024/1024

        self.get_logger().info(f"发布：{str(msg)}")
        self.status_publisher_.publish(msg)
def main():
    rclpy.init()
    rclpy.spin(SysStatusPub("sys_status_pub_node_py"))
    rclpy.shutdown()     

if __name__=='__main__':
    main()